//定义一个接口，service ,app ，card 的接口

function App() {
this.srcPrcMax = 50;
this.srcPrcMin = 1;
this.rang = 3;
this.dache = false;
this.duxiang = false;
}

App.prototype.打车 = function () {
    setFetchNodeMode(2, false, false, "nsf");
    setNodeDumpParam({"dumpMethod": 1, "maxDepth": 50});
    let node = textMatch(".*打车.*").getNodeInfo(10);
    if(node){
        //logd(JSON.stringify(node));
        //获取当前页面的节点父节点。
        for(let i= 0 ; i < node.length; i ++ ){
            let nod = node[i];
            //logd(JSON.stringify(nod))
            let 父节点 = nod.parent();
            if(父节点){
                for(let t = 0 ; t < 父节点.allChildren().length ; t ++){
                    let childnode = 父节点.child(t);
                    if(childnode.id === "com.jingyao.easybike:id/tvStartAddress"){
                        logd(childnode.text);
                        let rang =this.extractNumber(childnode.text) ;
                        logd("距离：",rang,"系统设定距离",form_json.range);
                        if(rang > form_json.range){
                            logd(childnode.text,"不符合当前距离");
                            break;
                        }
                    }
                    if(childnode.id === "com.jingyao.easybike:id/tvAmount"){
                        let prc = childnode.text;
                        //logd(prc)
                        //判断当前价格是否满足在设定的价格区间
                        if(prc <= form_json.srcPrcMax &&  prc >= form_json.srcPrcMin ){
                            sleep(300);
                            let result=  clickRandomRect(this.clickRandomRect(父节点.bounds.left+10, 父节点.bounds.top+10, 父节点.bounds.right-10, 父节点.bounds.bottom-10,"" ));
                            if(result){
                                logd("抢单成功");
                                let bool = true;
                                let runStatic = false;
                                while (bool){
                                    /*let node = id("com.jingyao.easybike:id/tvStartAddress").textMatch(".*距你.*").getNodeInfo(10);
                                    if(node){
                                        bool = false
                                        back();
                                        break;
                                    }*/
                                    let temp立即抢单 = this.立即抢单();
                                    if(temp立即抢单){
                                        temp立即抢单.click()
                                        runStatic = true;
                                    }else{
                                        logd("立即抢单没有查询到");
                                    }
                                    if(runStatic){
                                        this.到达上车地点();//程序执行了程序会停止。程序没有执行，会进入循环下一个轮回
                                    }
                                    sleep(300);
                                }
                            }
                        }
                    }
                }
            }

        }

    }else{
        logd("当前没有打车信息");
    }

}
/**
 * @description 正则表达式,用来获取数字
 * */
App.prototype.extractNumber = function (str) {
    const match = str.match(/\d+(\.\d+)?/);
    return match ? parseFloat(match[0]) : null;
}
Object.prototype.toString = function () {
    return JSON.stringify(this);
}

App.prototype.独享 = function () {
    setFetchNodeMode(2, false, false, "nsf");
    setNodeDumpParam({"dumpMethod": 1, "maxDepth": 50});
    ////id("com.jingyao.easybike:id/tvDriverPooling").getNodeInfo(10); //
    let node = textMatch(".*独享.*").getNodeInfo(10);
    if(node){
        //logd(node.length);
        //获取当前页面的节点父节点。
        for(let i= 0 ; i < node.length; i ++ ){
            let nod = node[i];
            let 父节点 =  nod.parent().allChildren();
            if(父节点){
                //logd(父节点.length)

                for(let t = 0 ; t < 父节点.length ; t ++){
                    let childnode = 父节点[t];

                    if(childnode.id === "com.jingyao.easybike:id/tvStartAddress"){
                        logd(childnode.text);
                        let rang =this.extractNumber(childnode.text) ;
                        logd("距离：",rang,"系统设定距离",form_json.range);
                        if(rang > form_json.range){
                            logd(childnode.text,"不符合当前距离");
                            break;
                        }
                    }
                    if(childnode.id === "com.jingyao.easybike:id/tvAmount"){
                        let prc = childnode.text;
                        logd(prc,"元")
                        //判断当前价格是否满足在设定的价格区间
                        if(prc <= form_json.srcPrcMax &&  prc >= form_json.srcPrcMin ){
                            sleep(300);
                           let result=  clickRandomRect(this.clickRandomRect(childnode.bounds.left, childnode.bounds.top, childnode.bounds.right, childnode.bounds.bottom,"" ));
                           if(result){
                                logd("抢单成功");
                                //开始判断当前菜单按钮是什么。
                                let bool = true;
                                let runStatic = false;
                                let run接单提醒确认同行 = false;
                                while (bool){
                                    let qingtengx = text("请保持端正").getOneNodeInfo(10);
                                    if(qingtengx){
                                        sleep(2000);
                                    }
                                    if(run接单提醒确认同行 === false){
                                        if(this.接单提醒确认同行() ){
                                            run接单提醒确认同行 = true;
                                            runStatic = true;
                                        }
                                        //logd("接单提醒确认同行 执行结果：" + run接单提醒确认同行);
                                        sleep(300);
                                        this.确定();
                                    }
                                    if(runStatic === false){
                                        let resultObject = this.立即抢单1();
                                        if(resultObject){
                                            resultObject.click();
                                            runStatic = true;
                                        }
                                        //logd("立即抢单1 执行结果：" + runStatic);
                                        if(this.查看其他订单()){
                                            bool = false
                                            break;
                                        }
                                    }
                                    if(runStatic){
                                       let temp = text("独享").getNodeInfo(10);
                                       if(temp){
                                           let tempnextduxiang = temp[temp.length-1].nextSiblings()[0].click();
                                           if(tempnextduxiang){
                                               let tempqueding = text("确定").getNodeInfo(10);
                                               if(tempqueding){
                                                   let tempnextduxiangbut = tempqueding[tempqueding.length-1].click();
                                                   if(tempnextduxiangbut){
                                                        //完成确定
                                                       sleep(400);
                                                   }
                                               }
                                           }
                                       }
                                       if(run接单提醒确认同行 === false){
                                           if(this.接单提醒确认同行() ){
                                               run接单提醒确认同行 = true;
                                           }
                                           //logd("接单提醒确认同行 执行结果：" + run接单提醒确认同行);
                                           sleep(300);
                                           this.确定();
                                       }
                                       if(run接单提醒确认同行){
                                           this.确定();
                                       }

                                        if(this.查看其他订单()){
                                            bool = false
                                            break;
                                        }

                                        if(this.到达上车地点()){
                                            if(this.查看其他订单()){
                                                bool = false
                                                break;
                                            }
                                        }
                                    }

                                   sleep(400);
                                }

                                return;

                            }
                        }
                    }
                }
            }else{
                sleep(2000);
                setFetchNodeMode(2, false, false, "nsf");
            }

        }

    }else{
        logd("当前没有独享信息");
    }

}

App.prototype.clickRandomRect = function (left, top, right, bottom, text) {
    let rect = new Rect();
    rect.left = left;
    rect.right = right;
    rect.top = top;
    rect.bottom = bottom;
    rect.text = text;
    logd(JSON.stringify(rect));
    return rect;
}

App.prototype.确认同行fun = function () {
    let node = text("确认同行").getOneNodeInfo(10);
    if(node){
        node.click();
        //sleep(1000);
        //this.查看其他订单();
        return true;
    }else{
        /*this.查看其他订单();
        sleep(1000);*/
        return false;
    }
}

App.prototype.确定 = function () {
    let node = text("确定").getOneNodeInfo(10);
    if(node){
        node.click();
        //utils.playMp3("/storage/emulated/0/Pictures/tishi.mp3", true)
        sleep(1000);
        this.继续pk返回();
        sleep(1000);
        this.pk失败返回();
        sleep(1000);
    }
}

App.prototype.继续pk返回 = function () {
    let node = text("继续PK更多订单").getOneNodeInfo(10);
    if(node){
        node.click();
        //utils.playMp3("/storage/emulated/0/Pictures/tishi.mp3", true)
        sleep(1000);

    }
}

App.prototype.查看其他订单 = function () {
    sleep(1000)
    let node = text("查看其他订单").getNodeInfo(10);
    if(node){
        node[node.length-1].click();
        //utils.playMp3("/storage/emulated/0/Pictures/tishi.mp3", true)
        sleep(1000);
        return true;
    }else{
        //logd("查看其他订单---->为空");
        return false;
    }
}

App.prototype.pk失败返回 = function () {
    let node = text("返回查看其他订单").getOneNodeInfo(10);
    if(node){
        node.click();
        //utils.playMp3("/storage/emulated/0/Pictures/tishi.mp3", true)
        sleep(1000);

    }
}

App.prototype.立即抢单 = function () {
    let node = text("立即抢单").getOneNodeInfo(10);
    if(node){
        return node;
    }else{
        //("立即抢单按钮没有看见");
        return null;
    }
}


App.prototype.立即抢单1 = function () {
    let node = text("立即抢单").getNodeInfo(10);
    if(node){
        return node[node.length-1];
    }else{
        //logd("立即抢单按钮没有看见");
        return false;
    }
}

App.prototype.接单提醒确认同行 = function () {
    let node = text("确认同行").getNodeInfo(10);
    if(node){
        return node[node.length-1].click();
    }else{
        //logd("接单提醒确认同行按钮没有看见");
        return false;
    }
}

App.prototype.刷新 = function () {
    logd("刷新");
    setFetchNodeMode(2, false, true, "nsf");
    setNodeDumpParam({"dumpMethod": 1, "maxDepth": 500});
    let node = id("com.jingyao.easybike:id/btnRefresh").getOneNodeInfo(10);
    if(node){
        //node[node.length-1].click();
        clickRandomRect(this.clickRandomRect(node.bounds.left+10, node.bounds.top+10, node.bounds.right-10, node.bounds.bottom-10,"" ));
        //utils.playMp3("/storage/emulated/0/Pictures/tishi.mp3", true)
    }else{
        logd("没有刷新按钮");
    }
}

App.prototype.到达上车地点 = function (){
    let node = id("com.jingyao.easybike:id/slide_switch_view_new").getOneNodeInfo(10)
    if(node){
        utils.playMp3("/storage/emulated/0/Pictures/tishi.mp3", true)
        sleep(20000)
        exit();
        return true;
    }else{
        return false;
    }

}


function init() {
    this.app = new App();
}

module.exports = new init()